#ifndef __PDIUSB_H__
#define __PDIUSB_H__

#include <AT89X52.H>
#include "typedef.h"

/* address line volage: high level,command
 *               low level, data*/
#define D12_COMMAND_ADD 1
#define D12_DATA_ADD    0  

/* bing_xing data */
#define D12_DATA P0
/* cmd or data select */
#define D12_A0   P3_5
#define D12_WR   P3_6
#define D12_RD   P3_7
#define D12_INT  P3_2

#define D12SetCommandAddr() (D12_A0=D12_COMMAND_ADD)
#define D12SetDataAddr()    (D12_A0=D12_DATA_ADD) 

#define D12SetRd()          (D12_RD=1)
#define D12ClrRd()          (D12_RD=0)

#define D12SetWr()          (D12_WR=1)
#define D12ClrWr()          (D12_WR=0)

#define D12GetIntPin()      (D12_INT)

#define D12GetData()        (D12_DATA)
#define D12SetData(Value)   (D12_DATA=(Value))

#define D12SetPortIn()      (D12_DATA=0xFF)
#define D12SetPortOut()

/* cmd to read PDIUSB-chip ID */
#define Read_ID    0xFD
#define D12_SET_MODE 0xF3
#define READ_INTERRUPT_REG 0xF4
#define D12_READ_BUFFER 0xF0
#define D12_CLEAR_BUFFER 0xF2
#define D12_ACKNOWLEDGE_SETUP 0xF1
#define D12_VALIDATA_BUFFER 0xFA
#define D12_WRITE_BUFFER 0xF0
#define D12_SET_ADDRESS_ENABLE 0xD0
#define D12_SET_ENDPOINT_ENABLE 0xD8

typedef struct {
	u8 case_num;
	int (*func)(void);
} case_func;

typedef struct {
	u8 case_num;
	int (*func)(u8);
} case_func_u8;

typedef struct {
	u8 case_num;
	int (*func)(u8*);
} case_func_pU8;

typedef struct {
	u8 case_num;
	u8 *desc;
} case_desc;

typedef struct {
	u8 SendLength;
	u8 *pData;
	u8 NeedZeroPkg;
} DataPkg;

extern case_func idata interruptCase[8];

void UsbInit(void);

void UsbDisconnect(void);
void UsbConnect(void);

void D12WriteCommand(u8 Command);
u8 D12ReadByte(void);
u16 D12ReadID(void);
void D12WriteByte(u8 u8data);

int UsbBusSuspend(void);
int UsbBusReset(void);
int UsbEp0Out(void);
int UsbEp0In(void);
int UsbEp1Out(void);
int UsbEp1In(void);
int UsbEp2Out(void);
int UsbEp2In(void);

void SendReport(void);

extern u8 idata hasSendHIDdesc;

#endif
